$(document)
    .ready(
        function ()
        {
            $("#from").datepicker(
            {
                defaultDate: "-1M",
                dateFormat: 'dd/mm/yy',
                changeMonth: true,
                changeYear: true,
				maxDate: 0,
                onClose: function (selectedDate)
                {
                    $("#to").datepicker("option", "minDate",
                        selectedDate);
                }
            });
            $("#to").datepicker(
            {
                changeMonth: true,
                dateFormat: 'dd/mm/yy',
                changeYear: true,
				maxDate: 0,
                onClose: function (selectedDate)
                {
                    $("#from").datepicker("option", "maxDate",
                        selectedDate);
                }
            });

            $("#myModal").modal();
            $("#from_modal").datepicker(
            {
                defaultDate: "-1M",
                changeMonth: true,
                dateFormat: 'dd/mm/yy',
                changeYear: true,
				maxDate: 0,
                onClose: function (selectedDate)
                {
                    $("#to_modal").datepicker("option",
                        "minDate", selectedDate);
                }
            });
            $("#to_modal").datepicker(
            {
                changeMonth: true,
                changeYear: true,
                dateFormat: 'dd/mm/yy',
				maxDate: 0,
                onClose: function (selectedDate)
                {
                    $("#from_modal").datepicker("option",
                        "maxDate", selectedDate);
                }
            });

            $("#establecer-modal").click(
                function ()
                {
                    if ($("#from_modal").val() == "" || $("#to_modal").val() == "")
                    {
                        $('#alerta').show();
                    }
                    else
                    {
                        $("#from").val($("#from_modal").val());
                        $("#to").val($("#to_modal").val());
                        $('#myModal').modal('hide');
                        generarReporte();
                    }
                });

            $("#Grid")
                .jqGrid(
                {
                    url: '/getReporte6?from=' + $('#from').val() + '&to=' + $('#to').val(),
                    datatype: 'json',
                    mtype: 'GET',
                    loadonce: true,
                    ajaxGridOptions:
                    {
                        contentType: 'application/json; charset=utf-8'
                    },
                    defaults:
                    {
                        recordtext: "Páh. {0} - {1} de {2}",
                        emptyrecords: "No existen datos",
                        loadtext: "Cargando...",
                        pgtext: "Pág {0} de {1}"
                    },
                    colNames: ['Servicio',
                        'Cotizaciones Aceptadas',
                        'Cotizaciones Rechazadas'
                    ],
                    colModel: [
                    {
                        name: 'servicio',
                        label: 'Servicio',
                        width: 250
                    },
                    {
                        name: 'aceptadas',
                        label: 'Aceptadas',
                        width: 125
                    },
                    {
                        name: 'rechazadas',
                        label: 'Rechazadas',
                        width: 125
                    }],
                    rowNum: 10,
                    rowList: [10, 20],
                    sortname: 'name',
                    sortorder: "asc",
                    pager: "#Pager",
                    viewrecords: true,
                    gridview: true,
                    width: 850,
                    height: 231,
                    rownumbers: true,
                    caption: 'CANTIDAD DE SOLICITUDES RECIBIDAS POR SERVICIO',
                    footerrow: true,
                    // onPaging: function () {
                    // generarReporte();
                    // return 'stop';
                    // },
                    loadComplete: function ()
                    {
                        var $self = $(this),
                            sum1 = $self.jqGrid("getCol", "aceptadas", false, "sum");
                        var $self = $(this),
                            sum2 = $self.jqGrid("getCol", "rechazadas", false, "sum");
                        $self.jqGrid("footerData", "set",
                        {
                            servicio: "Total:",
                            aceptadas: sum1,
                            rechazadas: sum2
                        });
                    }

                })

            jQuery("#Grid").jqGrid('navGrid', '#Pager',
            {
                edit: false,
                add: false,
                del: false,
                search: false
            });

        });

function generarReporte()
{
    var grid = $("#Grid");
    var url = '/getReporte6?from=' + $('#from').val() + '&to=' + $('#to').val()
    $("#Grid").jqGrid('setGridParam',
    {
        url: url
    });
    $("#Grid").setGridParam(
    {
        datatype: 'json',
        page: 1
    }).trigger('reloadGrid');

    var $self = grid,
        sum1 = $self.jqGrid("getCol", "aceptadas", false, "sum");
    var $self = grid,
        sum2 = $self.jqGrid("getCol", "rechazadas", false, "sum");

    $self.jqGrid("footerData", "set",
    {
        servicio: "Total:",
        aceptadas: sum1,
        rechazadas: sum2
    });
	return false;

}

function restablecer()
{
    $('#from').val("");
    $('#to').val("");
    generarReporte();
	return false;
}

function comeindex() {
	window.open('/index','_self',false);
}